<template>
  <div>
    <el-container style="height: 800px; border: 1px solid #eee">
      <el-aside width="250px" style="background-color: rgb(238, 241, 246)">
        <el-menu>
<!--        <el-menu router :default-openeds="['0', '1']">-->
<!--          <el-submenu v-for="(item,index) in menuTree" :index="index+''" v-if="item.show">-->
<!--            <template slot="title"><i class="el-icon-menu"></i>{{item.name}}</template>-->
<!--            &lt;!&ndash;              <el-menu-item route v-for="(item1,index1) in item.children" :index="index+'-'+index1">{{item1.name}}</el-menu-item>&ndash;&gt;-->
<!--            <el-menu-item v-for="(item1,index1) in item.children" :index="item1.path" :class="$route.path==item1.path?'is-active':''" v-if="item1.show">{{item1.name}}</el-menu-item>-->
<!--          </el-submenu>-->

          <menu-tree :dataList="menuData"></menu-tree>

        </el-menu>
      </el-aside>

      <el-menu>
        <router-view></router-view>
      </el-menu>

    </el-container>
  </div>
</template>

<script>
import MenuTree from "../components/MenuTree";
import router, {resetRouter} from "../router";
import {generateRoutes} from "../test";

var getRouter

export default {
  name: "Index",

  components:{
    'menu-tree':MenuTree
  },

  data() {
    return {
      menuData: [],
    }
  },

  created() {
    const _this = this
    if (localStorage.getItem("token")){
      axios.get('http://10.10.10.100:1650/getUserMenuAsyncWait').then(res => {

        console.log("data：", res.data.data)

        getRouter = res.data.data//后台拿到路由

        getRouter = generateRoutes([], getRouter)  // 过滤路由  转换路由的component

        console.log("getRouter：", getRouter)

        console.log("old_routers：", router.getRoutes())

        // generateRoutes方法是把后端返回的路由数据 转换为前端要的路由结构
        router.addRoute({ path: '*', redirect: '/404', hidden: true });
        router.addRoutes(getRouter) //动态添加路由
        console.log("new_routers：", router.getRoutes())

        global.antRouter = getRouter //将路由数据传递给全局变量，做侧边栏菜单渲染工作
        console.log("routers_menu：", global.antRouter)

        _this.menuData = global.antRouter
      })
    }
  }
}
</script>

<style scoped>

</style>